#!/usr/bin/evn python
# -*-coding:utf8 -*-
"""
画图 ： https://blog.csdn.net/ydyang1126/article/details/78842952
正统划分方法  ： https://blog.csdn.net/bing__liu/article/details/106573108
交叉验证    ： https://www.jianshu.com/p/42a2ba43a3e0
"""

# RandomForestClassifier
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from IPython.display import Image
from sklearn import datasets
from sklearn import tree
import pydotplus
import warnings
import os

# # 填补缺失值
# imp = Imputer(missing_values='NaN', strategy='mean', axis=0, verbose=0, copy=True)
# trainset1 = imp.fit_transform(trainset)

# 忽略一些版本不兼容等警告
warnings.filterwarnings("ignore")


# 提取身高体重信息优化
def presolve_data(csv_path):
    data = pd.read_csv(csv_path)
    data['体重下限'] = ""
    data['体重上限'] = ""
    data['身高下限'] = ""
    data['身高上限'] = ""
    column_list = list(data.columns)
    wmin_column = column_list.index('体重下限')
    wmax_column = column_list.index('体重上限')
    hmin_column = column_list.index('身高下限')
    hmax_column = column_list.index('身高上限')
    print(1111)
    # 判断身高体重
    find_number = r"\d{2,}\.?\d*"
    weight_key = ['斤', 'kg', 'KG', '以内', '公斤']
    height_key = ['码', '/', 'cm', '厘米', '（', '\(']
    height_re = r'(1[5-9](0|5))'
    clothes_re = r"([0-9-~尺—～]+(码|%s){0,1})" % ("|".join(weight_key + height_key))  # 分词序列中的元素进行区分
    weights_line = []
    heights_line = []
    for index, row in data.iterrows():
        height, weight = [], []
        if str(row["extra_info"]) != "nan":
            attribute_dict = json.loads( str(row["extra_info"]), strict=False)  # extra_info的非空字段组成的
            if 'sku' in attribute_dict.keys():
                for size_dict in attribute_dict['sku']:
                    all_info = [_[0] for _ in re.findall(clothes_re, str(size_dict["value"]))]  # 匹配含有尺码信息的value
                    if all_info:
                        print(':all_info',len(all_info))
                        for al in all_info:
                            for kw in weight_key:
                                if kw in al and al not in weight:
                                        weight.append(al)
                                        all_info.remove(al)
                        print(':after all_info', len(all_info))
                        ##############################
                        for al in all_info:
                            for hkw in height_key:
                                if hkw in al and al not in height:
                                    height += [_[0] for _ in re.findall(height_re, al)]
                    weights = [int(_) for _ in re.findall(find_number, " ".join(weight))]
                    heights = [int(_) for _ in re.findall(find_number, " ".join(height))]
                    if weights:
                        weights_line.append(index)
                        weights_min = min(weights)
                        weights_max = max(weights)
                        if 40 <= weights_min and weights_max <= 300:
                            if weights_min <= 70:
                                weights_min = 2 * weights_min
                                weights_max = 2 * weights_max
                            data.iat[index, wmin_column] = weights_min
                            data.iat[index, wmax_column] = weights_max
                    if heights:
                        heights_line.append(index)
                        heights_min = min(heights)
                        heights_max = max(heights)
                        if max(heights) < 200 and min(heights) > 140:
                            data.iat[index, hmin_column] = heights_min
                            data.iat[index, hmax_column] = heights_max
    data.to_csv(csv_path, index=0)
    cross_set = set(weights_line).intersection(set(heights_line))
    print('>>> cross:', cross_set,  '\n >>> length:', len(cross_set))
    print(">>> weight:", weights_line, '\n >>> length:', len(weights_line))
    print(">>> height:", heights_line, '\n >>> length:', len(heights_line))

"""
>>> cross: {2, 3, 4, 7, 2057, 11, 20, 21, 2072, 26, 2075, 30, 35, 37, 38, 39, 40, 42, 2093, 2095, 48, 2100, 2103, 2104, 61, 62, 63, 67, 68, 2116, 71, 72, 73, 2120, 75, 2126, 79, 2129, 87, 2136, 89, 90, 91, 92, 2140, 94, 95, 96, 2146, 101, 102, 103, 2149, 105, 2155, 2157, 110, 111, 112, 113, 114, 115, 2160, 2163, 123, 2172, 2173, 124, 125, 128, 2176, 130, 131, 132, 133, 137, 138, 139, 140, 2189, 2190, 2191, 144, 145, 2195, 148, 149, 2196, 2199, 152, 2200, 2201, 159, 160, 161, 2209, 2211, 164, 165, 166, 167, 2212, 2219, 172, 2223, 2225, 178, 179, 181, 2235, 188, 190, 2241, 194, 195, 196, 197, 200, 201, 2248, 2251, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 2260, 219, 220, 2265, 2274, 229, 2277, 231, 2278, 2280, 234, 235, 236, 237, 238, 239, 240, 241, 2285, 243, 244, 2288, 2294, 247, 249, 2298, 253, 254, 2303, 256, 257, 2305, 260, 2308, 2310, 2313, 266, 267, 268, 269, 270, 271, 2316, 273, 274, 275, 276, 277, 278, 279, 2324, 281, 282, 283, 2329, 2330, 2332, 287, 2333, 2335, 291, 2340, 2348, 2349, 303, 2358, 312, 313, 314, 315, 316, 317, 318, 2360, 320, 2364, 2366, 2369, 2370, 2371, 326, 327, 328, 2372, 2373, 331, 2377, 333, 334, 2379, 2380, 2383, 2384, 2385, 340, 341, 2386, 343, 344, 345, 346, 2387, 348, 349, 350, 2390, 2391, 2392, 2393, 2394, 2395, 357, 2399, 359, 360, 2400, 362, 2402, 364, 2404, 2405, 2406, 2407, 2408, 370, 371, 372, 2413, 2416, 375, 2417, 2418, 2420, 2422, 380, 2424, 2425, 2426, 384, 2427, 2431, 2432, 388, 389, 2434, 2439, 392, 393, 394, 395, 2444, 396, 2446, 397, 398, 399, 402, 403, 2443, 405, 406, 407, 408, 2450, 2452, 2453, 412, 413, 2455, 2459, 2465, 2466, 2468, 2470, 2471, 424, 2474, 2476, 429, 2478, 2480, 434, 2483, 436, 2486, 440, 2491, 446, 447, 450, 451, 453, 455, 2504, 460, 461, 462, 463, 2513, 466, 467, 468, 469, 2519, 2520, 2522, 475, 478, 2526, 480, 481, 482, 2527, 2529, 2531, 2532, 2534, 490, 2539, 2541, 494, 495, 496, 2545, 498, 499, 500, 501, 502, 2552, 512, 520, 525, 2574, 2575, 528, 2576, 2577, 2578, 2582, 2585, 2587, 2588, 4638, 2592, 2593, 2594, 2595, 548, 2596, 550, 551, 2597, 2604, 2605, 2608, 2609, 562, 2610, 2611, 2612, 2616, 569, 571, 2620, 573, 574, 581, 2631, 584, 2634, 587, 2638, 591, 2639, 2644, 2647, 601, 604, 605, 606, 2654, 608, 609, 2656, 612, 2662, 2664, 618, 621, 2670, 2671, 624, 2673, 628, 2677, 2678, 631, 632, 633, 634, 2679, 2684, 2689, 642, 2690, 645, 646, 647, 648, 649, 2693, 2702, 655, 2703, 2713, 2714, 677, 679, 680, 683, 684, 685, 2734, 690, 693, 696, 698, 701, 703, 706, 2755, 2761, 716, 717, 720, 722, 723, 730, 731, 734, 738, 740, 742, 747, 749, 753, 758, 760, 763, 765, 766, 767, 770, 776, 777, 778, 779, 781, 783, 784, 795, 798, 799, 801, 805, 806, 807, 808, 810, 826, 827, 828, 830, 831, 832, 833, 834, 838, 839, 841, 850, 858, 862, 864, 866, 867, 868, 870, 876, 878, 879, 4977, 896, 898, 900, 901, 905, 906, 910, 912, 917, 918, 929, 932, 934, 935, 936, 5035, 947, 962, 963, 964, 965, 977, 979, 980, 983, 984, 986, 987, 988, 990, 994, 995, 3043, 997, 1000, 1001, 1004, 1005, 1006, 1007, 1009, 1011, 1012, 1016, 1018, 1022, 1029, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1051, 1053, 1058, 1062, 1063, 1065, 1071, 1072, 1074, 1075, 1078, 5179, 1084, 1095, 1096, 1100, 1101, 1102, 1103, 5197, 1105, 5199, 1111, 1112, 1113, 1114, 1119, 1120, 1121, 1122, 3167, 5220, 1142, 1143, 1144, 1148, 1150, 1157, 1159, 1168, 1169, 1172, 1178, 1180, 1181, 1183, 1185, 1186, 1189, 3237, 1192, 1193, 1194, 1205, 1210, 1213, 1215, 1216, 1219, 1220, 1222, 1225, 1226, 1230, 1232, 1237, 1240, 1242, 1243, 1244, 1245, 1249, 1251, 1254, 1255, 1256, 1258, 1259, 1264, 1266, 1267, 1269, 1270, 1271, 1273, 1277, 1278, 1279, 1281, 1287, 1289, 1290, 1291, 1293, 1297, 1299, 1300, 1301, 1302, 1306, 1307, 1309, 1310, 1317, 1318, 1320, 1321, 1322, 1325, 1330, 1332, 1338, 1340, 1344, 3394, 1349, 1354, 1356, 1374, 1376, 1377, 1378, 1388, 1390, 1392, 1394, 1395, 1397, 1399, 1400, 1401, 1402, 1403, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1413, 1419, 1424, 1427, 1430, 1431, 1432, 1441, 1448, 1450, 1459, 1460, 1461, 1463, 1467, 1469, 1470, 1474, 1476, 1496, 1500, 1501, 1502, 1504, 1508, 1517, 1518, 1519, 1521, 1527, 1531, 1532, 1533, 1534, 1535, 1536, 1537, 1547, 1550, 1559, 1561, 1582, 1584, 1585, 1586, 1590, 1591, 1592, 1593, 1595, 1599, 1612, 1615, 1618, 1620, 1622, 1623, 1625, 1627, 1628, 1636, 1637, 1638, 1645, 1646, 1647, 1648, 1649, 1664, 1665, 1666, 1668, 1675, 1676, 1686, 1687, 1698, 1708, 1709, 1710, 1711, 1712, 1713, 1714, 1718, 1719, 1720, 1729, 1730, 1735, 1737, 1743, 1744, 1748, 1756, 1759, 1762, 1765, 1766, 1767, 1768, 1769, 2388, 1778, 1782, 1783, 1789, 1795, 1800, 1814, 1815, 1816, 2396, 1819, 1820, 1821, 1822, 1830, 1834, 1837, 1838, 2401, 3890, 1849, 1850, 1851, 1852, 1855, 1861, 1862, 1867, 1868, 1871, 1875, 2409, 2410, 1889, 1893, 1904, 1905, 2414, 1907, 1910, 3967, 1920, 1923, 1928, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1954, 1959, 1961, 1962, 1964, 1965, 4012, 4461, 1969, 1971, 1976, 1978, 1980, 1984, 1985, 1987, 1991, 2003, 2005, 2007, 2010, 2013, 2014, 2437, 2025, 2440, 2042, 2045} 
 >>> length: 913
>>> weight: [0, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 30, 31, 32, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 46, 48, 49, 50, 51, 52, 53, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 94, 95, 96, 100, 101, 102, 103, 105, 106, 107, 108, 109, 109, 109, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 178, 179, 181, 186, 188, 190, 194, 195, 196, 197, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 252, 253, 254, 256, 257, 260, 261, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 277, 277, 278, 279, 280, 281, 282, 283, 287, 289, 290, 291, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 305, 306, 312, 313, 314, 315, 316, 317, 318, 320, 321, 323, 324, 325, 326, 327, 328, 331, 332, 333, 334, 335, 340, 341, 343, 344, 345, 346, 347, 348, 349, 350, 352, 353, 356, 357, 358, 359, 359, 359, 360, 360, 360, 361, 362, 364, 368, 370, 371, 372, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 387, 388, 389, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 402, 403, 404, 405, 406, 407, 407, 407, 408, 408, 408, 409, 410, 412, 412, 413, 414, 415, 419, 421, 422, 423, 424, 429, 430, 431, 432, 434, 436, 437, 440, 441, 445, 446, 447, 448, 449, 450, 451, 452, 453, 455, 456, 457, 458, 460, 461, 462, 463, 465, 465, 466, 467, 468, 469, 470, 472, 473, 474, 475, 477, 478, 479, 480, 481, 482, 483, 487, 489, 489, 489, 490, 491, 491, 491, 492, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 503, 506, 507, 508, 509, 509, 509, 511, 512, 513, 515, 520, 521, 522, 524, 525, 526, 528, 529, 531, 532, 533, 534, 535, 536, 537, 538, 539, 539, 539, 539, 539, 540, 542, 544, 545, 546, 547, 547, 547, 548, 549, 550, 551, 552, 553, 555, 556, 556, 556, 556, 556, 558, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 573, 574, 575, 576, 577, 578, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 595, 595, 597, 599, 600, 600, 600, 601, 602, 604, 605, 606, 606, 606, 607, 608, 609, 610, 612, 613, 614, 615, 618, 619, 620, 621, 622, 624, 626, 628, 629, 630, 631, 632, 633, 634, 635, 637, 638, 639, 640, 642, 644, 645, 646, 647, 648, 649, 650, 651, 651, 653, 654, 655, 657, 661, 662, 663, 665, 667, 668, 669, 670, 671, 672, 673, 675, 677, 679, 680, 681, 683, 684, 685, 686, 688, 690, 691, 692, 693, 694, 695, 695, 695, 696, 698, 699, 700, 701, 703, 704, 705, 706, 707, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 729, 730, 731, 733, 734, 735, 738, 739, 740, 742, 743, 744, 745, 747, 748, 749, 751, 752, 753, 754, 755, 756, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 770, 772, 773, 774, 775, 776, 777, 778, 779, 781, 783, 784, 792, 794, 795, 796, 797, 798, 799, 800, 801, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 814, 816, 817, 818, 823, 824, 826, 827, 828, 830, 831, 832, 833, 834, 835, 837, 838, 839, 840, 841, 844, 845, 845, 846, 847, 848, 850, 851, 852, 853, 854, 855, 856, 858, 860, 861, 862, 863, 864, 865, 866, 867, 867, 867, 868, 869, 870, 871, 873, 874, 875, 876, 877, 878, 879, 881, 883, 884, 885, 887, 890, 891, 892, 893, 894, 896, 898, 899, 900, 901, 902, 905, 906, 907, 908, 910, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 923, 925, 926, 927, 928, 929, 930, 932, 933, 934, 935, 936, 940, 941, 942, 943, 944, 945, 947, 948, 949, 951, 952, 955, 957, 960, 962, 963, 964, 965, 967, 968, 969, 970, 971, 972, 972, 977, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1050, 1051, 1052, 1053, 1055, 1056, 1057, 1058, 1062, 1063, 1065, 1067, 1068, 1071, 1072, 1074, 1075, 1077, 1078, 1079, 1081, 1082, 1083, 1084, 1085, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1105, 1106, 1109, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1134, 1139, 1141, 1142, 1143, 1144, 1147, 1148, 1149, 1150, 1151, 1152, 1153, 1155, 1156, 1157, 1158, 1159, 1162, 1164, 1165, 1168, 1168, 1169, 1172, 1173, 1178, 1179, 1180, 1181, 1183, 1184, 1185, 1186, 1188, 1189, 1190, 1192, 1193, 1194, 1200, 1205, 1206, 1207, 1208, 1210, 1213, 1215, 1216, 1217, 1217, 1217, 1218, 1219, 1220, 1221, 1222, 1225, 1226, 1227, 1229, 1230, 1231, 1232, 1234, 1235, 1237, 1238, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 1251, 1252, 1253, 1254, 1255, 1256, 1258, 1259, 1260, 1260, 1261, 1263, 1264, 1265, 1266, 1267, 1269, 1270, 1271, 1273, 1275, 1277, 1278, 1279, 1280, 1281, 1282, 1283, 1284, 1287, 1288, 1289, 1290, 1291, 1293, 1295, 1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1306, 1307, 1308, 1309, 1310, 1311, 1312, 1313, 1317, 1318, 1320, 1321, 1322, 1323, 1324, 1325, 1329, 1330, 1331, 1332, 1334, 1335, 1336, 1338, 1340, 1341, 1342, 1343, 1344, 1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 1360, 1361, 1362, 1363, 1364, 1365, 1368, 1374, 1375, 1376, 1377, 1378, 1384, 1387, 1388, 1389, 1390, 1392, 1394, 1395, 1397, 1398, 1399, 1400, 1401, 1402, 1403, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1413, 1415, 1417, 1418, 1419, 1420, 1424, 1425, 1427, 1430, 1431, 1432, 1433, 1434, 1435, 1437, 1439, 1441, 1443, 1444, 1445, 1446, 1447, 1448, 1450, 1451, 1452, 1453, 1456, 1457, 1458, 1459, 1460, 1461, 1462, 1463, 1464, 1465, 1466, 1467, 1469, 1470, 1471, 1472, 1473, 1474, 1476, 1477, 1477, 1477, 1479, 1481, 1482, 1491, 1492, 1494, 1495, 1496, 1497, 1498, 1499, 1500, 1501, 1502, 1503, 1504, 1508, 1509, 1511, 1512, 1513, 1516, 1517, 1518, 1519, 1520, 1521, 1524, 1524, 1527, 1528, 1529, 1531, 1532, 1533, 1534, 1535, 1536, 1537, 1540, 1541, 1542, 1543, 1544, 1547, 1549, 1550, 1552, 1553, 1555, 1559, 1561, 1562, 1569, 1573, 1574, 1575, 1576, 1578, 1578, 1579, 1582, 1584, 1585, 1586, 1590, 1591, 1592, 1593, 1594, 1595, 1597, 1598, 1599, 1600, 1601, 1603, 1608, 1609, 1610, 1612, 1613, 1614, 1615, 1616, 1617, 1618, 1618, 1620, 1621, 1622, 1623, 1624, 1625, 1627, 1628, 1633, 1634, 1635, 1636, 1637, 1638, 1642, 1645, 1646, 1647, 1648, 1649, 1651, 1652, 1654, 1655, 1660, 1662, 1663, 1664, 1665, 1666, 1668, 1670, 1675, 1676, 1678, 1679, 1680, 1681, 1682, 1686, 1687, 1690, 1691, 1693, 1694, 1695, 1696, 1698, 1707, 1708, 1709, 1710, 1711, 1711, 1711, 1712, 1712, 1712, 1713, 1713, 1713, 1714, 1715, 1716, 1717, 1718, 1719, 1719, 1719, 1720, 1722, 1723, 1724, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1735, 1736, 1737, 1738, 1739, 1740, 1742, 1743, 1744, 1745, 1746, 1747, 1748, 1749, 1749, 1749, 1749, 1749, 1751, 1752, 1753, 1753, 1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1772, 1773, 1777, 1778, 1779, 1782, 1783, 1784, 1786, 1787, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1797, 1798, 1799, 1800, 1802, 1803, 1804, 1805, 1806, 1807, 1810, 1811, 1814, 1815, 1816, 1817, 1819, 1820, 1821, 1821, 1822, 1823, 1825, 1828, 1829, 1830, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1849, 1850, 1851, 1852, 1855, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1871, 1875, 1877, 1880, 1883, 1884, 1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1899, 1900, 1901, 1903, 1904, 1905, 1907, 1910, 1911, 1915, 1917, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1928, 1929, 1931, 1933, 1935, 1936, 1937, 1938, 1939, 1940, 1942, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1954, 1956, 1957, 1959, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1969, 1970, 1971, 1973, 1975, 1976, 1978, 1979, 1980, 1981, 1984, 1985, 1986, 1987, 1989, 1991, 1996, 1997, 1999, 2000, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017, 2018, 2020, 2021, 2023, 2024, 2025, 2028, 2029, 2030, 2031, 2035, 2036, 2037, 2040, 2041, 2042, 2045, 2046, 2048, 2049, 2050, 2051, 2052, 2053, 2055, 2057, 2058, 2059, 2061, 2062, 2063, 2065, 2067, 2069, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2100, 2102, 2103, 2104, 2105, 2106, 2109, 2110, 2112, 2113, 2115, 2116, 2120, 2124, 2125, 2126, 2127, 2128, 2129, 2130, 2133, 2134, 2135, 2136, 2137, 2138, 2139, 2140, 2141, 2142, 2143, 2145, 2146, 2147, 2148, 2149, 2150, 2151, 2152, 2153, 2154, 2155, 2156, 2156, 2157, 2159, 2160, 2161, 2163, 2164, 2166, 2167, 2168, 2169, 2172, 2173, 2173, 2174, 2175, 2176, 2176, 2176, 2176, 2177, 2178, 2181, 2182, 2182, 2183, 2184, 2184, 2185, 2186, 2187, 2189, 2190, 2191, 2193, 2194, 2195, 2196, 2196, 2196, 2199, 2200, 2201, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215, 2216, 2217, 2218, 2219, 2220, 2221, 2223, 2225, 2228, 2229, 2230, 2232, 2234, 2235, 2236, 2238, 2238, 2239, 2240, 2241, 2242, 2243, 2245, 2246, 2247, 2248, 2249, 2250, 2251, 2252, 2253, 2254, 2254, 2256, 2258, 2260, 2265, 2267, 2268, 2269, 2270, 2272, 2274, 2275, 2277, 2278, 2279, 2280, 2281, 2282, 2283, 2285, 2286, 2287, 2288, 2289, 2289, 2290, 2291, 2292, 2293, 2294, 2296, 2297, 2298, 2299, 2300, 2301, 2303, 2305, 2308, 2310, 2311, 2312, 2313, 2314, 2316, 2317, 2318, 2320, 2322, 2323, 2323, 2324, 2325, 2326, 2328, 2329, 2330, 2331, 2332, 2333, 2334, 2335, 2336, 2337, 2338, 2339, 2340, 2341, 2342, 2343, 2344, 2346, 2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358, 2359, 2360, 2361, 2362, 2363, 2364, 2365, 2366, 2367, 2368, 2369, 2370, 2371, 2372, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380, 2381, 2382, 2383, 2384, 2385, 2386, 2387, 2388, 2389, 2390, 2391, 2392, 2393, 2394, 2395, 2396, 2397, 2398, 2399, 2400, 2401, 2402, 2404, 2405, 2406, 2407, 2408, 2409, 2410, 2411, 2413, 2414, 2415, 2416, 2417, 2418, 2419, 2420, 2421, 2422, 2423, 2424, 2425, 2426, 2427, 2428, 2429, 2431, 2432, 2434, 2435, 2436, 2437, 2438, 2439, 2440, 2443, 2444, 2445, 2446, 2447, 2448, 2449, 2450, 2451, 2452, 2453, 2454, 2455, 2458, 2458, 2459, 2460, 2461, 2462, 2464, 2465, 2466, 2468, 2470, 2471, 2474, 2475, 2476, 2477, 2478, 2480, 2483, 2484, 2486, 2491, 2494, 2495, 2496, 2498, 2500, 2501, 2502, 2503, 2504, 2505, 2505, 2506, 2507, 2508, 2509, 2510, 2511, 2512, 2513, 2513, 2514, 2516, 2517, 2518, 2519, 2520, 2521, 2522, 2523, 2524, 2526, 2527, 2528, 2529, 2531, 2532, 2533, 2534, 2535, 2537, 2538, 2539, 2540, 2541, 2541, 2544, 2545, 2546, 2550, 2551, 2552, 2553, 2553, 2553, 2554, 2558, 2564, 2566, 2567, 2568, 2569, 2570, 2571, 2572, 2573, 2574, 2575, 2575, 2575, 2576, 2577, 2578, 2581, 2582, 2583, 2585, 2586, 2587, 2588, 2589, 2590, 2592, 2593, 2594, 2595, 2596, 2597, 2599, 2600, 2601, 2602, 2603, 2604, 2605, 2606, 2608, 2609, 2610, 2611, 2611, 2611, 2612, 2613, 2614, 2615, 2616, 2617, 2617, 2617, 2620, 2623, 2624, 2627, 2628, 2630, 2631, 2632, 2633, 2634, 2635, 2636, 2637, 2638, 2639, 2640, 2642, 2644, 2644, 2644, 2645, 2646, 2647, 2647, 2648, 2649, 2650, 2651, 2653, 2654, 2656, 2657, 2658, 2660, 2661, 2662, 2663, 2664, 2666, 2669, 2670, 2671, 2672, 2673, 2674, 2675, 2676, 2677, 2677, 2677, 2678, 2678, 2678, 2679, 2679, 2679, 2680, 2681, 2682, 2684, 2684, 2685, 2686, 2687, 2688, 2689, 2690, 2691, 2692, 2693, 2694, 2696, 2697, 2698, 2699, 2700, 2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2710, 2711, 2712, 2713, 2714, 2715, 2716, 2719, 2722, 2723, 2725, 2726, 2732, 2733, 2734, 2737, 2738, 2739, 2740, 2742, 2743, 2744, 2745, 2746, 2747, 2748, 2754, 2755, 2756, 2758, 2759, 2759, 2761, 2762, 2763, 2765, 2767, 2768, 2770, 2772, 2779, 2780, 2781, 2783, 2788, 2789, 2792, 2796, 2797, 2798, 2800, 2801, 2802, 2804, 2807, 2808, 2809, 2811, 2812, 2815, 2815, 2819, 2821, 2822, 2823, 2825, 2828, 2829, 2832, 2833, 2836, 2837, 2839, 2840, 2844, 2847, 2851, 2852, 2853, 2854, 2861, 2861, 2864, 2866, 2867, 2873, 2874, 2875, 2879, 2882, 2883, 2885, 2895, 2899, 2901, 2905, 2910, 2912, 2913, 2914, 2921, 2924, 2929, 2930, 2936, 2937, 2939, 2940, 2941, 2942, 2947, 2949, 2952, 2953, 2957, 2958, 2962, 2964, 2965, 2966, 2967, 2977, 2980, 2983, 2991, 2992, 3002, 3005, 3008, 3009, 3012, 3014, 3015, 3018, 3019, 3020, 3021, 3023, 3026, 3030, 3032, 3037, 3039, 3041, 3043, 3044, 3045, 3054, 3057, 3059, 3061, 3064, 3067, 3068, 3069, 3070, 3072, 3076, 3077, 3078, 3080, 3081, 3084, 3085, 3087, 3091, 3094, 3101, 3102, 3103, 3105, 3106, 3107, 3109, 3109, 3113, 3118, 3122, 3126, 3127, 3128, 3130, 3131, 3132, 3134, 3139, 3141, 3142, 3145, 3147, 3157, 3165, 3167, 3169, 3171, 3179, 3182, 3184, 3191, 3198, 3199, 3200, 3211, 3215, 3217, 3220, 3224, 3229, 3232, 3233, 3236, 3237, 3238, 3239, 3244, 3245, 3246, 3250, 3254, 3255, 3257, 3258, 3262, 3264, 3266, 3267, 3269, 3270, 3279, 3282, 3289, 3290, 3292, 3295, 3296, 3297, 3300, 3301, 3303, 3304, 3305, 3306, 3307, 3308, 3319, 3320, 3321, 3323, 3324, 3327, 3328, 3333, 3336, 3342, 3348, 3351, 3351, 3354, 3356, 3360, 3362, 3365, 3367, 3371, 3372, 3374, 3375, 3376, 3378, 3380, 3385, 3387, 3390, 3394, 3396, 3401, 3403, 3406, 3407, 3408, 3412, 3415, 3416, 3416, 3419, 3422, 3425, 3426, 3428, 3430, 3431, 3432, 3438, 3439, 3440, 3440, 3441, 3445, 3446, 3449, 3451, 3452, 3455, 3458, 3461, 3463, 3465, 3469, 3470, 3477, 3478, 3479, 3480, 3481, 3485, 3487, 3488, 3489, 3490, 3491, 3493, 3494, 3495, 3499, 3500, 3504, 3507, 3518, 3521, 3522, 3525, 3535, 3536, 3547, 3547, 3548, 3550, 3551, 3555, 3556, 3558, 3559, 3561, 3566, 3568, 3570, 3579, 3580, 3581, 3584, 3586, 3587, 3590, 3592, 3595, 3599, 3602, 3603, 3604, 3605, 3612, 3617, 3618, 3621, 3622, 3625, 3629, 3630, 3634, 3636, 3638, 3641, 3645, 3646, 3647, 3652, 3654, 3655, 3657, 3659, 3660, 3661, 3663, 3665, 3666, 3669, 3670, 3671, 3677, 3681, 3684, 3686, 3687, 3689, 3692, 3697, 3700, 3701, 3708, 3710, 3711, 3715, 3724, 3726, 3727, 3730, 3730, 3732, 3733, 3735, 3743, 3745, 3753, 3755, 3756, 3758, 3760, 3761, 3763, 3764, 3765, 3768, 3771, 3773, 3777, 3779, 3784, 3785, 3788, 3795, 3798, 3799, 3802, 3805, 3806, 3814, 3815, 3816, 3817, 3819, 3820, 3825, 3826, 3828, 3831, 3832, 3833, 3838, 3839, 3842, 3844, 3852, 3853, 3854, 3855, 3859, 3861, 3867, 3870, 3874, 3877, 3884, 3886, 3889, 3890, 3891, 3892, 3893, 3894, 3894, 3898, 3898, 3899, 3900, 3902, 3904, 3905, 3906, 3914, 3919, 3921, 3923, 3928, 3929, 3935, 3936, 3944, 3948, 3950, 3951, 3953, 3961, 3964, 3967, 3968, 3977, 3980, 3981, 3984, 3985, 3986, 3989, 3993, 3994, 3996, 4001, 4002, 4003, 4005, 4006, 4007, 4008, 4009, 4012, 4017, 4020, 4023, 4026, 4027, 4031, 4032, 4033, 4034, 4035, 4037, 4039, 4040, 4041, 4043, 4044, 4044, 4044, 4048, 4049, 4052, 4056, 4060, 4066, 4067, 4071, 4073, 4077, 4078, 4078, 4079, 4080, 4085, 4086, 4089, 4090, 4091, 4092, 4093, 4097, 4099, 4102, 4103, 4105, 4106, 4107, 4110, 4111, 4116, 4117, 4119, 4123, 4126, 4131, 4137, 4141, 4146, 4147, 4148, 4155, 4157, 4158, 4160, 4163, 4164, 4165, 4167, 4174, 4175, 4176, 4177, 4180, 4185, 4186, 4187, 4190, 4195, 4197, 4198, 4203, 4204, 4205, 4208, 4210, 4214, 4217, 4221, 4222, 4227, 4231, 4237, 4240, 4244, 4246, 4246, 4249, 4250, 4252, 4255, 4261, 4265, 4267, 4269, 4271, 4272, 4275, 4277, 4288, 4291, 4295, 4297, 4299, 4301, 4304, 4307, 4308, 4309, 4311, 4312, 4316, 4319, 4320, 4324, 4326, 4327, 4327, 4328, 4332, 4333, 4338, 4339, 4345, 4346, 4348, 4350, 4351, 4352, 4356, 4363, 4365, 4366, 4369, 4370, 4371, 4372, 4373, 4374, 4377, 4378, 4382, 4383, 4389, 4392, 4393, 4394, 4395, 4399, 4402, 4404, 4405, 4407, 4413, 4414, 4415, 4417, 4421, 4422, 4430, 4432, 4433, 4434, 4435, 4436, 4439, 4442, 4449, 4457, 4460, 4461, 4462, 4463, 4467, 4468, 4470, 4472, 4480, 4483, 4484, 4486, 4487, 4493, 4495, 4496, 4500, 4502, 4502, 4504, 4516, 4517, 4518, 4524, 4526, 4527, 4533, 4535, 4538, 4540, 4542, 4543, 4545, 4549, 4549, 4551, 4552, 4556, 4561, 4562, 4563, 4564, 4568, 4571, 4572, 4573, 4576, 4585, 4586, 4587, 4591, 4594, 4597, 4598, 4603, 4607, 4613, 4618, 4620, 4621, 4622, 4626, 4627, 4628, 4631, 4632, 4633, 4636, 4637, 4638, 4642, 4643, 4644, 4646, 4648, 4652, 4654, 4656, 4658, 4660, 4663, 4664, 4665, 4671, 4674, 4675, 4676, 4677, 4679, 4679, 4680, 4682, 4683, 4684, 4685, 4686, 4687, 4691, 4694, 4698, 4699, 4700, 4702, 4704, 4710, 4711, 4714, 4716, 4717, 4718, 4719, 4720, 4721, 4724, 4727, 4729, 4729, 4730, 4730, 4735, 4739, 4743, 4746, 4747, 4751, 4753, 4755, 4756, 4766, 4767, 4769, 4770, 4776, 4777, 4780, 4783, 4784, 4795, 4801, 4807, 4809, 4816, 4822, 4826, 4830, 4833, 4836, 4842, 4850, 4852, 4856, 4857, 4861, 4862, 4865, 4867, 4871, 4875, 4877, 4881, 4881, 4891, 4892, 4894, 4897, 4901, 4914, 4916, 4920, 4922, 4926, 4927, 4929, 4932, 4936, 4939, 4955, 4960, 4963, 4967, 4969, 4970, 4972, 4973, 4977, 4980, 4989, 4990, 4991, 4991, 4996, 5000, 5001, 5006, 5009, 5011, 5015, 5023, 5027, 5028, 5029, 5030, 5031, 5031, 5033, 5034, 5035, 5038, 5040, 5043, 5044, 5045, 5046, 5047, 5048, 5049, 5051, 5055, 5058, 5061, 5063, 5064, 5070, 5075, 5081, 5083, 5088, 5090, 5090, 5090, 5091, 5092, 5093, 5094, 5095, 5096, 5097, 5155, 5160, 5160, 5161, 5161, 5164, 5170, 5170, 5171, 5171, 5172, 5178, 5179, 5180, 5180, 5182, 5182, 5182, 5182, 5182, 5183, 5183, 5183, 5183, 5183, 5183, 5183, 5188, 5190, 5191, 5192, 5194, 5195, 5196, 5197, 5198, 5199, 5200, 5203, 5203, 5204, 5207, 5210, 5211, 5212, 5214, 5216, 5220, 5221, 5222, 5223] 
 >>> length: 3114
>>> height: [2, 3, 4, 7, 11, 20, 21, 26, 30, 35, 36, 37, 38, 39, 40, 42, 48, 61, 62, 63, 67, 68, 71, 72, 73, 75, 79, 87, 89, 90, 91, 92, 94, 95, 96, 101, 102, 103, 105, 110, 111, 112, 113, 114, 115, 123, 124, 125, 128, 130, 131, 132, 133, 137, 138, 139, 140, 144, 145, 148, 149, 152, 159, 160, 161, 164, 165, 166, 167, 172, 178, 179, 181, 188, 190, 194, 195, 196, 197, 200, 201, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 219, 220, 229, 231, 232, 234, 235, 236, 237, 238, 239, 240, 241, 243, 244, 247, 249, 253, 254, 256, 257, 260, 266, 267, 268, 269, 270, 271, 273, 274, 275, 276, 277, 277, 277, 278, 279, 281, 282, 283, 286, 287, 291, 303, 312, 313, 314, 315, 316, 317, 318, 320, 326, 327, 328, 331, 333, 334, 340, 341, 343, 344, 345, 346, 348, 349, 350, 357, 359, 359, 359, 360, 360, 360, 362, 364, 370, 371, 372, 373, 375, 380, 384, 388, 389, 392, 393, 394, 395, 396, 397, 398, 399, 402, 403, 405, 406, 407, 407, 407, 408, 408, 408, 412, 412, 413, 424, 429, 433, 434, 436, 438, 438, 438, 440, 446, 447, 450, 451, 453, 455, 460, 461, 462, 463, 466, 467, 468, 469, 475, 478, 480, 481, 482, 490, 494, 495, 496, 498, 499, 500, 501, 502, 512, 517, 520, 525, 528, 548, 550, 551, 554, 562, 569, 571, 573, 574, 581, 584, 587, 591, 601, 603, 604, 605, 606, 606, 606, 608, 609, 612, 618, 621, 624, 628, 631, 632, 633, 634, 642, 645, 646, 647, 648, 649, 655, 676, 677, 679, 680, 683, 684, 685, 690, 693, 696, 697, 698, 701, 703, 706, 716, 717, 720, 722, 723, 730, 731, 734, 738, 740, 742, 747, 749, 753, 758, 760, 763, 765, 766, 767, 768, 769, 770, 771, 776, 777, 778, 779, 781, 782, 783, 784, 786, 795, 798, 799, 801, 805, 806, 807, 808, 810, 821, 826, 827, 828, 830, 831, 832, 833, 834, 838, 839, 841, 842, 850, 858, 862, 864, 866, 867, 867, 867, 868, 870, 876, 878, 879, 886, 896, 898, 900, 901, 905, 906, 910, 912, 917, 918, 929, 932, 934, 935, 936, 937, 947, 962, 963, 964, 965, 966, 973, 977, 979, 980, 983, 984, 986, 987, 988, 990, 994, 995, 997, 1000, 1001, 1004, 1005, 1006, 1007, 1009, 1011, 1012, 1015, 1016, 1018, 1022, 1029, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1051, 1053, 1058, 1062, 1063, 1064, 1065, 1070, 1071, 1072, 1074, 1075, 1078, 1084, 1095, 1096, 1100, 1101, 1102, 1103, 1105, 1111, 1112, 1113, 1114, 1119, 1120, 1121, 1122, 1142, 1143, 1144, 1148, 1150, 1154, 1157, 1159, 1168, 1168, 1169, 1172, 1178, 1180, 1181, 1183, 1185, 1186, 1189, 1192, 1193, 1194, 1195, 1195, 1198, 1198, 1199, 1204, 1205, 1209, 1210, 1213, 1215, 1216, 1219, 1220, 1222, 1225, 1226, 1230, 1232, 1237, 1240, 1242, 1243, 1244, 1245, 1249, 1251, 1254, 1255, 1256, 1257, 1258, 1259, 1264, 1266, 1267, 1269, 1270, 1271, 1273, 1276, 1277, 1278, 1279, 1281, 1285, 1287, 1289, 1290, 1291, 1293, 1297, 1299, 1300, 1301, 1302, 1306, 1307, 1309, 1310, 1317, 1318, 1320, 1321, 1322, 1325, 1330, 1332, 1333, 1338, 1340, 1344, 1345, 1349, 1354, 1356, 1372, 1374, 1376, 1377, 1378, 1388, 1390, 1392, 1393, 1394, 1395, 1397, 1399, 1400, 1401, 1402, 1403, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1413, 1414, 1416, 1416, 1419, 1424, 1427, 1428, 1428, 1430, 1431, 1432, 1440, 1441, 1448, 1450, 1459, 1460, 1461, 1463, 1467, 1469, 1470, 1474, 1476, 1483, 1496, 1500, 1501, 1502, 1504, 1505, 1508, 1514, 1517, 1518, 1519, 1521, 1527, 1531, 1532, 1533, 1534, 1535, 1536, 1537, 1547, 1550, 1556, 1559, 1561, 1582, 1584, 1585, 1586, 1587, 1588, 1590, 1591, 1592, 1593, 1595, 1599, 1612, 1615, 1618, 1618, 1620, 1622, 1623, 1625, 1627, 1628, 1636, 1637, 1638, 1645, 1646, 1647, 1648, 1649, 1664, 1665, 1666, 1668, 1675, 1676, 1686, 1687, 1688, 1689, 1698, 1708, 1709, 1710, 1711, 1711, 1711, 1712, 1712, 1712, 1713, 1713, 1713, 1714, 1718, 1719, 1719, 1719, 1720, 1729, 1730, 1735, 1737, 1743, 1744, 1748, 1750, 1756, 1759, 1762, 1765, 1766, 1767, 1768, 1769, 1778, 1782, 1783, 1789, 1795, 1800, 1801, 1808, 1814, 1815, 1816, 1819, 1820, 1821, 1821, 1822, 1830, 1834, 1837, 1838, 1843, 1849, 1850, 1851, 1852, 1855, 1856, 1861, 1862, 1867, 1868, 1871, 1875, 1878, 1889, 1893, 1904, 1905, 1907, 1910, 1920, 1923, 1928, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1954, 1959, 1961, 1962, 1964, 1965, 1969, 1971, 1976, 1978, 1980, 1984, 1985, 1987, 1991, 1995, 2003, 2005, 2007, 2010, 2013, 2014, 2025, 2042, 2044, 2045, 2057, 2072, 2075, 2093, 2095, 2100, 2103, 2104, 2108, 2116, 2120, 2123, 2126, 2129, 2136, 2140, 2146, 2149, 2155, 2157, 2160, 2163, 2165, 2172, 2173, 2173, 2176, 2176, 2176, 2176, 2180, 2188, 2189, 2190, 2191, 2195, 2196, 2196, 2196, 2199, 2200, 2201, 2209, 2211, 2212, 2219, 2223, 2225, 2235, 2241, 2248, 2251, 2260, 2263, 2265, 2274, 2277, 2278, 2280, 2285, 2288, 2294, 2298, 2303, 2305, 2308, 2309, 2310, 2313, 2316, 2324, 2329, 2330, 2332, 2333, 2335, 2340, 2348, 2349, 2358, 2360, 2364, 2366, 2369, 2370, 2371, 2372, 2373, 2377, 2379, 2380, 2383, 2384, 2385, 2386, 2387, 2388, 2390, 2391, 2392, 2393, 2394, 2395, 2396, 2399, 2400, 2401, 2402, 2404, 2405, 2406, 2407, 2408, 2409, 2410, 2413, 2414, 2416, 2417, 2418, 2420, 2422, 2424, 2425, 2426, 2427, 2431, 2432, 2434, 2437, 2439, 2440, 2443, 2444, 2446, 2450, 2452, 2453, 2455, 2457, 2459, 2465, 2466, 2467, 2468, 2470, 2471, 2473, 2474, 2476, 2478, 2480, 2483, 2485, 2486, 2487, 2491, 2492, 2504, 2513, 2513, 2519, 2520, 2522, 2525, 2526, 2527, 2529, 2531, 2532, 2534, 2539, 2541, 2541, 2545, 2547, 2549, 2552, 2556, 2557, 2559, 2574, 2575, 2575, 2575, 2576, 2577, 2578, 2582, 2585, 2587, 2588, 2592, 2593, 2594, 2595, 2596, 2597, 2604, 2605, 2608, 2609, 2610, 2611, 2611, 2611, 2612, 2616, 2620, 2621, 2626, 2629, 2631, 2634, 2638, 2639, 2641, 2644, 2644, 2644, 2647, 2647, 2654, 2655, 2655, 2656, 2662, 2664, 2668, 2670, 2671, 2673, 2677, 2677, 2677, 2678, 2678, 2678, 2679, 2679, 2679, 2684, 2684, 2689, 2690, 2693, 2702, 2703, 2713, 2714, 2728, 2734, 2755, 2761, 2845, 2846, 2889, 2986, 2986, 3043, 3167, 3204, 3237, 3241, 3291, 3316, 3361, 3391, 3394, 3890, 3915, 3967, 4012, 4461, 4630, 4630, 4638, 4781, 4781, 4977, 4988, 4988, 5035, 5124, 5127, 5134, 5168, 5179, 5197, 5199, 5208, 5220] 
 >>> length: 1076
"""

# 提取鞋子尺码优化
def get_shoes_data(shoe_path):
    data = pd.read_csv(shoe_path)
    data['鞋码下限'] = ""
    data['鞋码上限'] = ""
    shoes_re = r'[^\d]([3][2-9]|[4][0-9])[^\d]'
    column_list = list(data.columns)
    wmin_column = column_list.index('鞋码下限')
    wmax_column = column_list.index('鞋码上限')
    for index, row in data.iterrows():
        extra_info = str(row["extra_info"]).replace("\\", "")
        attribute_dict = json.loads(extra_info, strict=False)  # extra_info的非空字段组成的
        if 'sku' in attribute_dict.keys():
            for size_dict in attribute_dict['sku']:
                shoes_sizes = re.findall(shoes_re, str(size_dict['value']))
                print('鞋码：：', shoes_sizes)
                if len(shoes_sizes) > 1:  # 鞋码出现肯定有最大最小，如果只有一个，可能是门牌或者价钱等干扰
                    data.iat[index, wmin_column] = min(shoes_sizes)  # 对这个单元格赋值
                    data.iat[index, wmax_column] = max(shoes_sizes)
    data.to_csv(shoe_path, index=0)


# 随机森林
def clsfy_rf_grid():

    csv_path = './clo/clo_4full.csv'
    # csv_path = './clo/clo_he.csv'
    # csv_path = './clo/clo_we.csv'

    data = pd.read_csv(csv_path)
    print(data)

    map = {'男': 1, '女': 2, '中性': 0}
    class_name = ['1', '2', '0']
    data['type'] = data['type'].map(map)

    # 三种特征搭配
    feature = ['min_weight', 'max_weight', 'min_height', 'max_height']
    # feature = ['min_height', 'max_height']
    # feature = ['min_weight', 'max_weight']

    y = np.array(data['type'])
    # x是图像的特征，去掉type那列
    x = data.drop('type', axis=1)

    rfc = RandomForestClassifier(criterion='gini', n_estimators=1000)  # 随机森林
    # dtc = DecisionTreeClassifier(max_depth=4)
    # 用gini减少对数运算
    depth = [2, 4, 6, 8]
    model = GridSearchCV(rfc, param_grid={'max_depth': depth}, cv=5)
    # 网格搜索来调节参数，就是调节最大的深度分别是什么的时候，效果最好
    model.fit(x, y)
    print('特征组合:', feature)
    print('最佳参数:', model.best_params_)  # 最佳参数用于画图
    scores_rfc = cross_val_score(model, x, y, cv=10, scoring='accuracy')
    print('准确率为:%.2f%%' % (scores_rfc.mean() * 1000))
    best_params = list((model.best_params_).values())
    print(best_params)
    # 开始画图
    rf = RandomForestClassifier(criterion='gini', n_estimators=100, max_depth=int(best_params[0]), random_state=40)  # 确定随机参数，否则每次运行模型结果不同
    rf.fit(x, y)
    draw_arg_flowsheet(rf, feature, class_name)


# 决策树
def clsfy_dt_grid():
    # csv_path = './clo/clo_4full.csv'
    # csv_path = './clo/clo_he.csv'
    # csv_path = './clo/clo_we.csv'
    csv_path = './shoe/sho_1_no_null.csv'


    data = pd.read_csv(csv_path)
    print(data)

    map = {'男': 1, '女': 2, '中性': 0,'无法分辨':3}
    class_name = ['1', '2', '0']
    data['type'] = data['type'].map(map)

    # 三种特征搭配
    # feature = ['min_weight', 'max_weight', 'min_height', 'max_height']
    # feature = ['min_height', 'max_height']
    # feature = ['min_weight', 'max_weight']
    feature = ['min_sho', 'max_sho']

    y = np.array(data['type'])
    # x是图像的特征，去掉type那列
    x = data.drop('type', axis=1)

    scores_plt = {}
    depth = [2, 4, 6, 8]
    for d in range(2, 8):
        dtc = DecisionTreeClassifier(criterion='gini', max_depth=d)
        scores = cross_val_score(dtc, x, y, scoring='accuracy', cv=10)
        scores_plt[scores.mean() * 100] = d
        print('深度为%s' % d, '准确率：', scores.mean() * 100)

    best_score = max(scores_plt.keys())
    print(best_score)
    best_depth = scores_plt[best_score]
    print('最佳深度为：', best_depth)

    # 开始画图

    dtc = DecisionTreeClassifier(criterion='entropy', max_depth=int(best_depth), random_state=0)
    dtc.fit(x, y)
    dot_data = tree.export_graphviz(dtc, out_file=None,
                                    feature_names=feature,
                                    class_names=class_name,
                                    filled=True, rounded=True,
                                    special_characters=True)
    graph = pydotplus.graph_from_dot_data(dot_data)
    # 使用ipython的终端jupyter notebook显示。
    Image(graph.create_png())
    # 如果没有ipython的jupyter notebook，可以把此图写到pdf文件里，在pdf文件里查看。
    graph.write_pdf((os.path.splitext(csv_path))[0] + "_决策树.pdf")

"""
    最佳组合
    特征组合: ['min_weight', 'max_weight', 'min_height', 'max_height']
    最佳参数: {'max_depth': 4}
    准确率为:98.25%
    feature_names
        ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
    class_names:
        ['setosa' 'versicolor' 'virginica']
"""


# 画随机森林参数重要性和决策流程图
def draw_arg_flowsheet(model, feature, class_name):

    # 画决策路径
    """
    Estimators = model.estimators_
    for index, model in enumerate(Estimators, start=0):
        filename = '特征数量为%s' % len(feature) + str(index) + '.pdf'
        dot_data = tree.export_graphviz(model, out_file=None,
                                        feature_names=feature,
                                        class_names=class_name,
                                        filled=True, rounded=True,
                                        special_characters=True)
        graph = pydotplus.graph_from_dot_data(dot_data)
        # 使用ipython的终端jupyter notebook显示。
        Image(graph.create_png())
        graph.write_pdf(filename)
"""
    # 画特征重要性
    y_importances = model.feature_importances_
    x_importances = feature
    y_pos = np.arange(len(x_importances))
    # 横向柱状图
    plt.barh(y_pos, y_importances, align='center')
    plt.yticks(y_pos, x_importances)
    plt.xlabel('Importances')
    plt.xlim(0, 1)
    plt.title('freature num is %s ,Freatures Importances' %len(feature))
    plt.show()

    # 竖向柱状图
    plt.bar(y_pos, y_importances, width=0.4, align='center', alpha=0.4)
    plt.xticks(y_pos, x_importances)
    plt.ylabel('Importances')
    plt.ylim(0, 1)
    plt.title('freature num is %s ,Freatures Importances' %len(feature))
    plt.show()


# 纯正随机森林
def pure_rf():
    # csv_path = './clo/clo_4full.csv'
    # csv_path = './clo/clo_he.csv'
    # csv_path = './clo/clo_we.csv'

    data = pd.read_csv(csv_path)
    print(data)

    map = {'男': 1, '女': 2, '中性': 0}
    class_name = ['1', '2', '0']
    data['type'] = data['type'].map(map)

    # 三种特征搭配
    feature = ['min_weight', 'max_weight', 'min_height', 'max_height']
    # feature = ['min_height', 'max_height']
    # feature = ['min_weight', 'max_weight']
    y = np.array(data['type'])
    # x是图像的特征
    x = data.drop('type', axis=1)
    print('特征数为：', x.shape)
    # test_size表明测试集占整体样本的比例；random_state是随机数种子，当为0时，
    # 每次生成的随机数即随机顺序是不相同的，当为固定的数值时，可以在多次运行时获得相同的随机数。
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=40)
    # 构建随机森林，其中n_estimators表示决策树的棵树。
    rf = RandomForestClassifier(n_estimators=100, random_state=40, max_depth=4, criterion='gini')
    # 对训练集的特征及正确分类结果进行训练
    rf.fit(x_train, y_train)
    # 根据测试集的特征对分类结果进行预测
    predictions = rf.predict(x_test)
    # 输出测试集的预测结果以及原本的正确分类结果。
    # for prediction, test_label in zip(predictions, y_test):
    #     pass
    # [print(len(prediction))]
    # 获得预测准确率
    score = rf.score(x_test, y_test)
    print('accuracy is\n ', score)



if __name__ == '__main__':

    # clsfy_rf_grid()
    clsfy_dt_grid()
    # pure_rf()


